package client.comm.baoding.ui

import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.adapter.GoodDetailImgAdapter
import client.comm.baoding.api.bean.GoodsDetail
import client.comm.baoding.databinding.ActivityGoodDetailimgBinding
import client.comm.baoding.ui.vm.TestViewModel
import client.comm.commlib.base.SimpleBannerAdapter
import client.comm.commlib.base.SyBannerAdapter
import client.comm.commlib.getScreenWidth
import client.comm.commlib.utils.isNotNull
import com.bumptech.glide.request.RequestOptions
import com.youth.banner.indicator.CircleIndicator

class GoodDetailImgsActivity : BaseMiddleActivity<TestViewModel, ActivityGoodDetailimgBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_good_detailimg
    }

    val adapter: GoodDetailImgAdapter by lazy {
        GoodDetailImgAdapter(this, mImgLoader)
    }

    override fun initView() {
        binding?.apply {
            event = this@GoodDetailImgsActivity
            var linMana = LinearLayoutManager(this@GoodDetailImgsActivity)
//            linMana.isSmoothScrollbarEnabled = true
//            recyclerView.layoutManager = linMana
//            recyclerView.setHasFixedSize(true)
//            recyclerView.isNestedScrollingEnabled = false
            recyclerView.adapter = adapter

        }
        val width = getScreenWidth(this)
        val params: ViewGroup.LayoutParams = binding.banner.getLayoutParams()
        params.height = width //(width * 575 / 1400f).toInt()
        binding.banner.setLayoutParams(params)
        binding.banner.setIndicator(CircleIndicator(this))

    }

    override fun initData() {
        var bannerList = intent.getParcelableArrayListExtra<GoodsDetail.Banner>("banner_list")
        val detailList = intent.getParcelableArrayListExtra<GoodsDetail.Detail>("detail_list")

        if (bannerList != null) {
            binding.banner.setAdapter(object : SimpleBannerAdapter<GoodsDetail.Banner>(bannerList) {
                override fun onBindView(
                    holder: BannerViewHolder?,
                    data: GoodsDetail.Banner?,
                    position: Int,
                    size: Int
                ) {
                    mImgLoader.load(data?.goods_img)
                        .apply(RequestOptions().error(R.mipmap.ic_launcher)).into(
                            holder!!.imageView
                        )
                }
            })
        }
        binding.banner.start()

        val list = ArrayList<String>()
        detailList?.let {
            for (d in it) {
                if (d != null) {
                    list.add(d.goods_img)
                }
            }
        }
        adapter.submitList(list)

    }

    override fun initViewModel(): TestViewModel = createViewModel()

}